package com.zzh.partnersys;

import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.Map;

public class AmapLocationWithApacheHttpClient {
    public static void main(String[] args) {
        // 替换为你的高德地图API Key
        String apiKey = "66f0747300645759892c9af3a339b955";
        String ip = "114.247.50.2";
        String url = "https://restapi.amap.com/v3/ip?key=" + apiKey + "&ip=" + ip;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        try {
            CloseableHttpResponse response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity);
                Gson gson = new Gson();
                Map<String, Object> map = gson.fromJson(result, Map.class);
                String rectangle = (String) map.get("rectangle");
                System.out.println("rectangle: " + rectangle);
//                System.out.println("entity: " + entity);
                System.out.println("响应内容: " + result);
            } else {
                System.out.println("请求失败，状态码: " + statusCode);
            }
        } catch (IOException e) {
            System.out.println("发生错误: " + e.getMessage());
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                System.out.println("关闭HttpClient出错: " + e.getMessage());
            }
        }
    }
}